package com.glodon.utils.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * DataTable方法集合
 * Created by kongdm-a on 2014-8-8.
 */

public class DataTableUtils {
    /**
     * 从请求中获取DataTable参数
     * @param request
     * @return
     */
    public static DataTableParameter getDataTableParameter(HttpServletRequest request) {
        DataTableParameter dataTableParameter = new DataTableParameter();
        Map<String, String[]> params = request.getParameterMap();
        dataTableParameter.setsEcho(params.get("sEcho")[0]);
        dataTableParameter.setiColumns(Integer.valueOf(params.get("iColumns")[0]));
        dataTableParameter.setsColumns(params.get("sColumns")[0]);
        dataTableParameter.setiDisplayStart(Integer.valueOf(params.get("iDisplayStart")[0]));
        dataTableParameter.setiDisplayLength(Integer.valueOf(params.get("iDisplayLength")[0]));
        dataTableParameter.setiSortingCols(Integer.valueOf(params.get("iSortingCols")[0]));
        dataTableParameter.setsSearch(params.get("sSearch")[0]);
        dataTableParameter.setbRegex(Boolean.valueOf(params.get("bRegex")[0]));
        String smDataProp = "mDataProp_";
        String ssSearch = "sSearch_";
        String sbRegex = "bRegex_";
        String sbSearchable = "bSearchable_";
        String sbSortable = "bSortable_";
        String siSortCol = "iSortCol_";
        String ssSortDir = "sSortDir_";
        String[] mDataProps = new String[dataTableParameter.getiColumns()];
        String[] sSearchs = new String[dataTableParameter.getiColumns()];
        Boolean[] bRegexs = new Boolean[dataTableParameter.getiColumns()];
        Boolean[] bSearchables = new Boolean[dataTableParameter.getiColumns()];
        Boolean[] bSortables = new Boolean[dataTableParameter.getiColumns()];
        Integer[] iSortCols = new Integer[dataTableParameter.getiSortingCols()];
        String[] sSortDirs = new String[dataTableParameter.getiSortingCols()];
        for (int i = 0; i < dataTableParameter.getiColumns(); i++) {
            mDataProps[i] = params.get(smDataProp+i)[0];
            sSearchs[i] = params.get(ssSearch+i)[0];
            bRegexs[i] = Boolean.valueOf(params.get(sbRegex+i)[0]);
            bSearchables[i] = Boolean.valueOf(params.get(sbSearchable+i)[0]);
            bSortables[i] = Boolean.valueOf(params.get(sbSortable+i)[0]);
        }
        for (int i = 0; i < dataTableParameter.getiSortingCols(); i++) {
            iSortCols[i] = Integer.valueOf(params.get(siSortCol+i)[0]);
            sSortDirs[i] = params.get(ssSortDir+i)[0];
        }

        dataTableParameter.setmDataProps(mDataProps);
        dataTableParameter.setsSearchs(sSearchs);
        dataTableParameter.setbRegexs(bRegexs);
        dataTableParameter.setbSearchables(bSearchables);
        dataTableParameter.setbSortables(bSortables);
        dataTableParameter.setiSortCols(iSortCols);
        dataTableParameter.setsSortDirs(sSortDirs);
        return dataTableParameter;
    }
}
